package com.yimi.appstore.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageStatsObserver;
import android.content.pm.PackageManager;
import android.content.pm.PackageStats;
import android.os.RemoteException;
import android.util.Log;

public class AppSizeUtil {
	private static final String TAG = "AppSizeUtil";

	public long queryPacakgeSize(Context context, String pkgName)
			throws Exception {
		if (pkgName != null) {
			// 使用放射机制得到PackageManager类的隐藏函数getPackageSizeInfo
			PackageManager pm = context.getPackageManager(); // 得到pm对象
			try {
				// 通过反射机制获得该隐藏函数
				Method getPackageSizeInfo = pm.getClass().getDeclaredMethod(
						"getPackageSizeInfo", String.class,
						IPackageStatsObserver.class);
				// 调用该函数，并且给其分配参数 ，待调用流程完成后会回调PkgSizeObserver类的函数
				PkgSizeObserver pso = new PkgSizeObserver();
				getPackageSizeInfo.invoke(pm, pkgName, pso);
				while (!pso.isGetStats) {
					Thread.sleep(1000);
				}
				PackageStats pStats = pso.pStats;
				// long cachesize = pStats.cacheSize ; //缓存大小
				 long datasize = pStats.dataSize ; //数据大小
				long codesize = pStats.codeSize; // 应用程序大小
				// long totalsize = cachesize + datasize + codesize ;
				// Log.i(TAG,
				// "cachesize--->"+cachesize+" datasize---->"+datasize+
				// " codeSize---->"+codesize);
				return codesize+datasize;
			} catch (Exception ex) {
				Log.e(TAG, "" + ex);
				Log.e(TAG, "NoSuchMethodException");
				ex.printStackTrace();
				throw ex; // 抛出异常
			}
		}
		return 0;
	}

	public int queryCanMoveApk(Context context, String pkgName)
			throws Exception {
		if (pkgName != null) {
			// 使用放射机制得到PackageManager类的隐藏函数getPackageSizeInfo
			PackageManager pm = context.getPackageManager(); // 得到pm对象
			try {
				// 通过反射机制获得该隐藏函数
				Field installlocation = pm.getApplicationInfo(pkgName, 8192)
						.getClass().getDeclaredField("installLocation");
				int i = (Integer) installlocation.get(pm.getApplicationInfo(
						pkgName, 8192));
				if (i == 0) {
					Field dir = pm.getApplicationInfo(pkgName, 8192).getClass()
							.getDeclaredField("sourceDir");
					String path = (String) dir.get(pm.getApplicationInfo(
							pkgName, 8192));
					if(path.contains("/mnt/")){
						i = 1;
					}
				}
				return i;
			} catch (Exception ex) {
				Log.e(TAG, "" + ex);
				Log.e(TAG, "NoSuchMethodException");
				ex.printStackTrace();
				throw ex; // 抛出异常
			}
		}
		return -1;
	}

	// aidl文件形成的Bindler机制服务类
	public class PkgSizeObserver extends IPackageStatsObserver.Stub {
		public PackageStats pStats;
		public boolean isGetStats = false;

		/***
		 * 回调函数，
		 * 
		 * @param pStatus
		 *            ,返回数据封装在PackageStats对象中
		 * @param succeeded
		 *            代表回调成功
		 */
		@Override
		public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)
				throws RemoteException {
			this.pStats = pStats;
			isGetStats = true;
			// long cachesize = pStats.cacheSize ; //缓存大小
			// long datasize = pStats.dataSize ; //数据大小
			// long codesize = pStats.codeSize ; //应用程序大小
			// long totalsize = cachesize + datasize + codesize ;
			// Log.i(TAG, "cachesize--->"+cachesize+" datasize---->"+datasize+
			// " codeSize---->"+codesize) ;
		}
	}
}
